/**
 * @author zhounan 
 * @date 2024年6月25日
 * @version V1.0
 */
package org.zn.quality.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zn.etl.job.dao.EtlJobMapper;
import org.zn.etl.job.entity.EtlJob;
import org.zn.etl.job.entity.JobGroup;
import org.zn.etl.job.entity.po.EtlJobPo;
import org.zn.etl.misc.BeanCopierUtils;
import org.zn.etl.service.EtlPersistService;
import org.zn.quality.dao.QualitySampleRuleMapper;
import org.zn.quality.dao.QualityStatisticsColumnRuleMapper;
import org.zn.quality.dao.QualityStatisticsRuleMapper;
import org.zn.quality.dao.QualityStrategyInfoMapper;
import org.zn.quality.entity.QualityStrategyEntity;
import org.zn.quality.entity.po.QualitySampleRulePo;
import org.zn.quality.entity.po.QualityStatisticsColumnRulePo;
import org.zn.quality.entity.po.QualityStatisticsRulePo;
import org.zn.quality.entity.po.QualityStrategyInfoPo;
import org.zn.quality.service.QualityManagerService;

/**
 * @author zhounan 
 * @date 2024年6月25日
 * @version V1.0
 */

@Service
public class QualityManagerServiceImpl implements QualityManagerService {

	@Autowired
	private QualityStrategyInfoMapper qualityStrategyInfoPoMapper;
	@Autowired
	private QualitySampleRuleMapper qualitySampleRuleMapper;
	@Autowired
	private QualityStatisticsColumnRuleMapper qualityStatisticsColumnRuleMapper;
	@Autowired
	private QualityStatisticsRuleMapper qualityStatisticsRuleMapper;
	@Autowired
	private EtlJobMapper etlJobMapper;
	
	@Autowired
	private EtlPersistService etlPersistService;
	
	
	public QualityStrategyEntity queryStrategyEntityById(Long id) {
		
		QualityStrategyInfoPo qsi=  qualityStrategyInfoPoMapper.selectByPrimaryKey(id);
		
		QualityStrategyEntity qse=BeanCopierUtils.copy(qsi, new QualityStrategyEntity());
		
		if("sample".equals(qse.getRuleType())) {
			
			qse.setSampleRule( qualitySampleRuleMapper.selectByStrategyId(id));
			
		}
		else if("statistics".equals(qse.getRuleType())) {
			
			QualityStatisticsRulePo qsr= qualityStatisticsRuleMapper.selectByStrategyId(qse.getId());
			
			qse.setStatisticsRule(qsr);
			
			List<QualityStatisticsColumnRulePo>  cols= qualityStatisticsColumnRuleMapper.selectByStatisticsRuleId(qsr.getId());
			qse.setStatisticsColumnRule(cols);
			
		}
		
		return qse;
		
	}
	
	
	@Transactional(rollbackFor = Exception.class)
	public Long save(QualityStrategyEntity save) {
		
		String rtype=save.getRuleType();
		if(rtype==null)save.setRuleType("tableCount");

		if(save.getId()==null) {
			//save.setId(UuidUtils.generateIdString());
			save.setCreateTime(new Date());
			qualityStrategyInfoPoMapper.insert(save);
		}else {
			qualityStrategyInfoPoMapper.updateByPrimaryKey(save);
			
		}
				
		if("sample".equals(rtype)) {
			
			QualitySampleRulePo  sr=	save.getSampleRule();
			
			sr.setStrategyId(save.getId());
			
			if(sr.getId()==null) {
				//sr.setId(UuidUtils.generateIdString());
				qualitySampleRuleMapper.insert(sr);
			}else {
				qualitySampleRuleMapper.updateByPrimaryKey(sr);
			}
			
		}
		else if("statistics".equals(rtype)) {
			
			 List<QualityStatisticsColumnRulePo>  columnRules=save.getStatisticsColumnRule(); 
			
			 QualityStatisticsRulePo  statisticsRule=save.getStatisticsRule();

			 statisticsRule.setStrategyId(save.getId());
			 
			 if(statisticsRule.getId()==null) {
				 
				 //statisticsRule.setId(UuidUtils.generateIdString());
				 qualityStatisticsRuleMapper.insert(statisticsRule);
				 
			 }else {
				 qualityStatisticsRuleMapper.updateByPrimaryKey(statisticsRule);

			 }
			 
			 for (QualityStatisticsColumnRulePo col : columnRules) {
				
				 col.setStatisticsRuleId(statisticsRule.getId());
				 //col.setId(UuidUtils.generateIdString());
				 
				 qualityStatisticsColumnRuleMapper.insert(col);

			 }
			
		}
		

		return save.getId();

	}
	
	
		public List<QualityStrategyEntity> queryStrategyByJobId(Long jobId){
		
		List<QualityStrategyInfoPo> straList= qualityStrategyInfoPoMapper.selectByJob(jobId);
		
		List<QualityStrategyEntity> entityList=new ArrayList<>();
		
		for (QualityStrategyInfoPo stra : straList) {
			
			QualityStrategyEntity qse=BeanCopierUtils.copy(stra, new QualityStrategyEntity());
			
			entityList.add(qse);
			
			if("sample".equals(qse.getRuleType())) {
				
				qse.setSampleRule( qualitySampleRuleMapper.selectByStrategyId(qse.getId()));
				
			}
			else if("statistics".equals(qse.getRuleType())) {
				
				
				QualityStatisticsRulePo qsr= qualityStatisticsRuleMapper.selectByStrategyId(qse.getId());
				
				qse.setStatisticsRule(qsr);
				
				List<QualityStatisticsColumnRulePo>  cols= qualityStatisticsColumnRuleMapper.selectByStatisticsRuleId(qsr.getId());
				qse.setStatisticsColumnRule(cols);
				
			}
			
		}
		return entityList;

	}
	
	
	
	
	
	public List<QualityStrategyEntity> queryJobStrategy(String jobName){
		
		EtlJobPo  job= etlJobMapper.selectByName(jobName); 
		if(job==null) {
			return null;
		}		
		
		Long  jobId=	job.getId();		
		return queryStrategyByJobId(jobId);

	}
	
	public int  delStrategy(Long id){
		
		return qualityStrategyInfoPoMapper.deleteByPrimaryKey(id);
	
	}
	
	public int  clearJobStrategy(Long jobId){
		
		return qualityStrategyInfoPoMapper.clearJobStrategy(jobId);
	
	}


	@Override
	public List<Long> batchAdd(QualityStrategyEntity ent) {
		// TODO Auto-generated method stub
		
		JobGroup group=etlPersistService.queryJobGroupBy(ent.getGroupName());
		
		List<EtlJob> jobs=group.getJobList();
		
		String batchName= ent.getName();
		
		List<Long> ids=new ArrayList<>();
		
		for (EtlJob etlJob : jobs) {
			
			ent.setName(etlJob.getJobName()+"_"+batchName+"_Strategy");
			ent.setJobId(etlJob.getId());
			ent.setId(null);
			
			if( ent.getSampleRule()!=null) {
				ent.getSampleRule().setId(null);
			}
			
			if( ent.getStatisticsRule()!=null) {
				ent.getStatisticsRule().setId(null);
			}
			
			Long id=save(ent);
			ids.add(id);
			
		}

		return ids;
	}


	@Override
	public List<QualityStrategyInfoPo> selectAvailableByType(String ruleType) {
		
		List<QualityStrategyInfoPo> list= qualityStrategyInfoPoMapper.selectAvailableByType(ruleType);
		
		return list;
	}
	
	
	
	
	
	
}
